from rest_framework import serializers

from goods.models import SKU
from orders.models import OrderInfo, OrderGoods


class SKUSeriaizer(serializers.ModelSerializer):
    """
    商品sku表序列化器
    """

    class Meta:
        model = SKU
        fields = ('name', 'default_image')


class OrderGoodsSerializer(serializers.ModelSerializer):
    """
    订单商品序列化器
    """
    # 子表嵌套父表返回
    sku = SKUSeriaizer()

    class Meta:
        model = OrderGoods
        fields = ('count', 'price', 'sku')


class OrderInfoSerializer(serializers.ModelSerializer):
    """
        订单基本信息序列化器
    """
    # 关联嵌套序列化返回 子表嵌套父表返回
    user = serializers.StringRelatedField(read_only=True)
    user_id = serializers.IntegerField(read_only=True)
    # 父表嵌套子表返回
    skus = OrderGoodsSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = '__all__'
